Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ELF metadata #88

Closed
wants to merge 4 commits into from
Closed

Update ELF metadata #88

wants to merge 4 commits into from

Conversation

agrojean-ledger
Copy link
Contributor

@agrojean-ledger agrojean-ledger commented Oct 6, 2023

Update infos.rs (add more metadatas to match C SDK)

Still missing :

  • APPNAME and APPVERSION : don't know how to fetch them at compile time of the SDK...
  • SDK_VERSION and SDK_NAME : fields are set to 'None' but we will give them a proper value when we transition to the new rust SDK with https://github.com/LedgerHQ/secure-sdk-rust

@agrojean-ledger agrojean-ledger force-pushed the update-app-metadata branch 2 times, most recently from 483b180 to 8697436 Compare October 11, 2023 16:20
build.rs Outdated
// scott could use for_each
command.define(define, None);
}
extract_defines_from_cx_makefile(&mut command, &cx_makefile);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this for better readability but we can remove it.. Not related to what this PR is supposed to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in #96

@agrojean-ledger agrojean-ledger requested a review from yhql October 11, 2023 16:24
@agrojean-ledger
Copy link
Contributor Author

agrojean-ledger commented Oct 12, 2023

@yhql I am missing APPNAME and APPVERSION, do you know how to add them when the rust SDK's build.rs is called ?

src/infos.rs Outdated
Comment on lines 36 to 44
static ELF_API_LEVEL: [u8;4] = if cfg!(target_os = "nanos"){
*b"0\n\0\0"
} else if cfg!(target_os = "nanox") {
*b"5\n\0\0"
} else if cfg!(target_os = "nanosplus") {
*b"1\n\0\0"
} else {
*b"255\n"
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would need to be retrieved from the SDK, but this for after the change of architecture I guess?
Also, to be consistent with C sdk, the value for nanos should be : undefined, e.g. section doesn't exist.
and the else value might be the same ? Or a build error cause that's just invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes in the new SDK we will grab it from the C SDK, it is already done actually here https://github.com/LedgerHQ/secure-sdk-rust/blob/f78cb5803b47de24ef7b2b0323b8b08ad31f7399/build.rs#L180

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96

Comment on lines +48 to +49
static ELF_TARGET: [u8; 8] = if cfg!(target_os = "nanos") {
*b"nanos\n\0\0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there solutions to avoid these trailing "\n\0"? It seems a bit weird having them everywhere as I guess htey won't be used by the tools reading elf sections?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

We could use the following code:

let mut elf_target: [u8; 8] = [0u8; 8];
let _ = &mut elf_target[..5].copy_from_slice("nanos".as_bytes());   

but cannot be called to define static const 😞 (and not sure it enhances readability)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I propose to keep it as it is currently (until better proposal)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering how these trailing \n and \0 will affect our tools reading metadata? Mostly Speculos and LedgerBlue for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #96

@@ -5,6 +5,7 @@
#![test_runner(testing::sdk_test_runner)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
#![feature(const_cmp)]
Copy link
Contributor

@yogh333 yogh333 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not find this nightly feature in Unstable Book.

  1. How did you find it ?
  2. Where is it used ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yogh333 it's for let min_length = core::cmp::min(bytes.len(), MAX_METADATA_STRING_SIZE - 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But with @yhql 's proposed solution for strings, we probably would not need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for how I found it, I can't remember ^^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this if we replace this min by a more manual version:

   let min_length = if bytes.len() <= MAX_METADATA_STRING_SIZE - 1 {
        bytes.len()
    } else {
        MAX_METADATA_STRING_SIZE - 1
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in #96

@yogh333 yogh333 closed this Nov 14, 2023
@yogh333 yogh333 deleted the update-app-metadata branch November 14, 2023 08:18
@yogh333 yogh333 restored the update-app-metadata branch November 16, 2023 21:52
@yogh333 yogh333 reopened this Nov 16, 2023
Comment on lines +34 to +44
#[used]
#[link_section = "ledger.api_level"]
static ELF_API_LEVEL: [u8; 4] = if cfg!(target_os = "nanos") {
*b"0\n\0\0"
} else if cfg!(target_os = "nanox") {
*b"5\n\0\0"
} else if cfg!(target_os = "nanosplus") {
*b"1\n\0\0"
} else {
*b"255\n"
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be update now that API_LEVEL is now at build time from SDK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96

result
}

/// Expose the API_LEVEL to the app
#[used]
static API_LEVEL: u8 = if cfg!(target_os = "nanos") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be update to with proper value knwon at build time ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96


#[used]
#[link_section = "ledger.sdk_hash"]
static ELF_SDK_HASH: [u8; 5] = *b"None\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can live with this, but best would be having the hash of the C SDK used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96


#[used]
#[link_section = "ledger.sdk_name"]
static ELF_SDK_NAME: [u8; 18] = *b"ledger-secure-sdk\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of a nano, with should probably set nanos-secure-sdk as it doesn't use the unified one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96


#[used]
#[link_section = "ledger.sdk_version"]
static ELF_SDK_VERSION: [u8; 5] = *b"None\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for the hash, would be best to have the real C SDK version, else that is not a drama.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #96

Copy link
Contributor

@xchapron-ledger xchapron-ledger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some stuff could be up^date now that we use ledger_secure_sdk_sys crate.
We could use the env declared here: https://github.com/LedgerHQ/ledger-device-rust-sdk/blob/master/ledger_secure_sdk_sys/build.rs#L208-L210
And maybe add some more to populate properly SDK_HASH / SDK_NAME and SDK_VERSION ?

@agrojean-ledger
Copy link
Contributor Author

Replaced by #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants